<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/26 15:58
 * Remark:
 */

namespace App\Models\Staff;

use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;
use App\Models\BaseModelV2;


class StaffDepartmentModel extends BaseModelV2
{
    protected $table = TableConsts::CY_STAFF_DEPARTMENT_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_STAFF_DEPARTMENT_TABLE_PRIMARY_KEY;

    protected $fillable = [
        "staff_department_id",
        "staff_id",
        "department_id",
        "position_id",
        "menus"
    ];

    public $timestamps = false;

    /**
     * 新增部门用户关联
     * @param $staffDepartmentData
     * @return mixed
     */
    public function addStaffDepartment($staffDepartmentData): mixed
    {
        return $this->insertGetId($staffDepartmentData);
    }

    protected static function booted(){}

    public function isExistByCondition($conditionData){
        return $this->withoutGlobalScopes()
            ->firstOrnew($conditionData)->exists;
    }

    /**
     * 根据用户名获取职位列表
     * @param $staffId
     * @return mixed
     */
    public function getStaffDepartments($staffId): mixed
    {
        return $this
            ->select([
                'staff_department.staff_department_id',
                'staff_department.department_id',
                'staff_department.position_id',
                'staff_department.position_id as position_name_str',
                'd.department_name',
                'staff_department.is_master',
            ])
            ->leftJoin(TableConsts::CY_DEPARTMENT_TABLE_NAME." as d",'d.department_id','staff_department.department_id')
            ->where('staff_id',$staffId)
            ->get();
    }

    public function getStaffDepartmentMenus($staffId,$departmentId){
        return $this
            ->select([
                'staff_department.menus',
            ])
            ->where('staff_id',$staffId)
            ->where('department_id',$departmentId)
            ->first();
    }


    /**
     * 根据用户名获取职位列表
     * @param $staffId
     * @return mixed
     */
    public function getStaffDepartmentsByLogin($staffId): mixed
    {
        return $this
            ->select([
                'staff_department.staff_department_id',
                'staff_department.department_id',
                'staff_department.position_id',
                'staff_department.position_id as position_name_str',
                'd.department_name',
            ])
            ->leftJoin(TableConsts::CY_DEPARTMENT_TABLE_NAME." as d",'d.department_id','staff_department.department_id')
            ->where('staff_id',$staffId)
            ->get();
    }


    /**
     * 通过主键获取信息
     * @param $staffDepartmentId
     * @return mixed
     */
    public function getStaffDepartmentInfoByStaffDepartmentId($staffDepartmentId): mixed
    {
        return $this
            ->select([
                'staff_department.staff_department_id',
                'staff_department.department_id',
                'staff_department.position_id',
                'd.department_name',
                'staff_department.position_id as position_name_str',
            ])
            ->leftJoin(TableConsts::CY_DEPARTMENT_TABLE_NAME." as d",'d.department_id','staff_department.department_id')
            ->where('staff_department_id',$staffDepartmentId)
            ->first();
    }

    /**
     * 获取部门count
     * @param $staffId
     * @return mixed
     */
    public function getStaffDepartmentCount($staffId): mixed
    {
        return $this
            ->select([
                'staff_department.staff_department_id',
            ])
            ->where('staff_id', $staffId)
            ->count();
    }

    /**
     * 修改员工部门职位
     * @param $wheres
     * @param $staffDepartmentData
     * @return mixed
     */
    public function editStaffDepartment($wheres,$staffDepartmentData): mixed
    {
        return $this->where('staff_id',$wheres['staffId'])
            ->where('staff_department_id',$wheres['staffDepartmentId'])
            ->update($staffDepartmentData);
    }

    public function editStaffMasterByStaffId($staffId){
        return $this->where('staff_id',$staffId)
            ->update([
                'is_master' => 2
            ]);
    }


    /**
     * 删除部门职位
     * @param $wheres
     * @return mixed
     */
    public function deleteStaffDepartment($wheres): mixed
    {
        return $this->where('staff_id',$wheres['staffId'])
            ->where('staff_department_id',$wheres['staffDepartmentId'])
            ->delete();
    }
}